package view.thread;
import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;

/**
 *
 * @author
 * Guerino
 */
public class IndexThread extends Thread{
    private String totalFiles;
    private String totalFolder;
    private String totalSize;
    private String startIndex;
    private String endIndex;    
    private File pathFolder;
    private JProgressBar progress;
    private JTextArea txtArea;
    
    public IndexThread(File pathFolder, JProgressBar progress, JTextArea txtAreaInfo) {
        this.pathFolder = pathFolder;
        this.progress  = progress;
        this.txtArea  = txtAreaInfo; 
        //Mensajes a utilizar
        this.totalFiles  = "Total de archivos: ";
        this.totalFolder = "Total de directorios: ";
        this.totalSize   = "Tamaño total en disco: ";
        this.startIndex = "Iniciando indizacion..........";
        this.endIndex   = ".........Indizacion finalizada";
    }

    
    @Override
    public void run() {
        try {          
            Thread.sleep(100); 
            this.txtArea.setText("");
            long startTime = System.currentTimeMillis();
            this.progress.setVisible(true);
            //System.out.println("Indexación iniciada: " + startTime/1000);
            this.txtArea.append(startIndex + "\n");
            //Actualiza la posicion del cursor
            this.txtArea.setCaretPosition(this.txtArea.getDocument().getLength()-1);
            
            //Gestor de indizacion
            
            
            long endTime = System.currentTimeMillis();
            this.txtArea.append(endIndex + "\n");
            //Actualiza la posicion del cursor
            this.txtArea.setCaretPosition(this.txtArea.getDocument().getLength()-1);            
        
            double total = (double)(startTime - endTime)/(double)1000;
            this.txtArea.append("Tiempo transcurrrido: " + total + " seg" + "\n");
            //Actualiza la posicion del cursor
            this.txtArea.setCaretPosition(this.txtArea.getDocument().getLength()-1); 
            
            this.txtArea.append(totalFiles + " " + "\n");
            this.txtArea.setCaretPosition(this.txtArea.getDocument().getLength()-1); 
            
            this.txtArea.append(totalFolder + " " + "\n");
            this.txtArea.setCaretPosition(this.txtArea.getDocument().getLength()-1); 
            
            this.txtArea.append(totalSize + " " + "\n");
            this.txtArea.setCaretPosition(this.txtArea.getDocument().getLength()-1); 
            
            //Finalizo la tarea, se oculta la barra de progreso 
            this.progress.setVisible(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, this.pathFolder + " ", "Error", JOptionPane.WARNING_MESSAGE);
        } 
    }
    
    
}
